package jastgen.generator;

import static jastgen.generator.Helper.*;

import java.io.PrintWriter;
import java.util.*;

import jastgen.CompileException;
import jastgen.nodes.ClassDef;
import jastgen.nodes.InterfaceDef;
import jastgen.nodes.Module;


class ValueVisitorStubBodyGenerator implements BodyGenerator {

	private final List<InterfaceDef> interfaceNames;
	private final Module node;

	ValueVisitorStubBodyGenerator(
			final List<InterfaceDef> interfaceNames,
			final Module node
		) throws CompileException {

		this.interfaceNames = interfaceNames;
		this.node = node;
	}

	public void generate(
			final PrintWriter out)
	{
		generateVisitorBody(out, node);
	}

	private void generateVisitorBody(
			final PrintWriter out,
			final Module node)
	{
		out.println("public abstract class ValueVisitorStub<T> implements ValueVisitor<T> {");

		for(final ClassDef c : node.getClassDefs()) {
			printMethod(out, c, "T");
		}

		// interfacesの出力
		for(final InterfaceDef t : interfaceNames)
			printVisit(out, t);

		out.println("}");
	}

	private void printVisit(
			final PrintWriter out,
			final InterfaceDef n)
	{
		final String x = n.getTypeParamsString();
		final String y = n.getBaseName();
		final String z = n.getName();
		out.println();
		out.println("	public final " + x + "T in" + y + "(");
		out.println("			final " + z + " n)");
		printNullableVisit(out);
	}
}
